// Appendix8.3
log using Appendix8.3.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\BES2019_W27_v29.1.dta" // Fieldhouse, E., J. Green, G. Evans, J. Mellon, C. Prosser, J. Bailey, R. de Geus, H. Schmitt, C. van der Eijk, J. Griffiths, & S. Perrett. (2024) British Election Study Internet Panel Waves 1-29. DOI: 10.5255/UKDA-SN-8202-2 // Accessed March 11 2025

// Social justice scale
* Clean "don't know" responses
foreach var in cwTrans cwAuthors cwLanguage cwTraining {
    replace `var' = . if `var' == 9999
}

*Reverse variable so social justice coded high
foreach var in cwLanguage cwTraining {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in cwTrans cwAuthors rcwLanguage rcwTraining {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in scwTrans scwAuthors srcwLanguage srcwTraining {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen socJusValues = .
replace socJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale reverse
qui sum al_scale
local max_value = r(max)
gen reversed_al_scale = `max_value' - al_scale

// Measures of democracy
* Clean don't know responses
foreach var in strongLeader populism1 populism2 populism4 populism5 populism6 {
    replace `var' = . if `var' == 9999
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step. Include strongleader question so this item has a 1-2 scale, same as other dependent variables in the book
foreach var in strongLeader populism1 populism2 populism4 populism5 populism6 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*At this point, the populism scale has a Cronbach's alpha of 0.77

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in spopulism1 spopulism2 spopulism4 spopulism5 spopulism6 {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scorePOP = 0
gen count_nonzeroPOP = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in spopulism1 spopulism2 spopulism4 spopulism5 spopulism6 {
    replace total_scorePOP = total_scorePOP + `var'
    replace count_nonzeroPOP = count_nonzeroPOP + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen Populism = .
replace Populism = total_scorePOP / count_nonzeroPOP if count_nonzeroPOP > 0

rename sstrongLeader StrongLeader

// Demographics
*Delete missing values and rename
replace p_gross_household = . if inlist(p_gross_household, 16, 17) // missing about 25%
rename gender FemaleGender

*Generate dummies
gen MinorityEthnic=. 
replace MinorityEthnic=0 if inlist(p_ethnicity, 1, 2)
replace MinorityEthnic=1 if inrange(p_ethnicity, 3, 15)

gen Graduate=.
replace Graduate=0 if inrange(p_edlevelUni, 0, 3)
replace Graduate=1 if inrange(p_edlevelUni, 4, 5)

// Standardize
egen Age = std(age)
egen Income = std(p_gross_household)
egen LibValues = std(reversed_al_scale)
egen SocJusValues = std(socJusValues)


// Regressions
regress Populism SocJusValues [pweight=wt], robust 
eststo
regress Populism SocJusValues Age FemaleGender Graduate Income MinorityEthnic [pweight=wt], robust 
eststo
regress StrongLeader SocJusValues Age FemaleGender Graduate Income MinorityEthnic [pweight=wt], robust 
eststo
regress Populism SocJusValues Age FemaleGender Graduate Income MinorityEthnic LibValues [pweight=wt], robust 
eststo
regress StrongLeader SocJusValues Age FemaleGender Graduate Income MinorityEthnic LibValues [pweight=wt], robust 
eststo
esttab

log close

eststo clear

esttab using "Appendix8.3.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit Populism SocJusValues Age FemaleGender Graduate Income MinorityEthnic SocCulEmployment [pweight=wt], robust 

// Coefficient plot
regress StrongLeader SocJusValues Age FemaleGender Graduate Income MinorityEthnic LibValues [pweight=wt], robust 
label variable Age ""
label variable FemaleGender ""
label variable Income ""
label variable SocJusValues ""
label variable LibValues ""
coefplot, drop(_cons) xline(0, lcolor(red) lwidth(medium)) scheme(white_jet) xtitle("{bf: Support for autocracy}")

